var demo;
$(function() {
	//开启验证
	demo = $("#editForm").Validform({
		tiptype: 3
	});
	addDialogButton("保存",save);

	laydate.render({
	    trigger:'click',
	    elem: '#sub_time', //指定元素
	    done:function(value, date){
	    	$('.week').html(getWeek(value));
	    }
	});
	$('.week').html(getWeek($('#sub_time').val()));
});

var save = function() {
	if(!demo.check()) {
		$('.Validform_error:first').focus();
		return;
	}
	var datas = getFormJson();
	if($('#id').val()==""){
		showDialogModalConfirm('confirm','请输入文件名称','<input type="text" class="new_name" style="background-color:#eee; text-indent:5px;"/><script>$(".new_name").focus()</script>' ,'确认','取消',function(){ 
			var load_alert=loadAlert('保存中...');
			datas.name=$('.new_name').val();
			if(datas.name==''){
				window.parent.topMessagePrompt('error','请填写文件名');
				return;
			}
			$.ajax({
				url: "Admin/SupervisionOther/tableSave2",
				type: "post",
				data: datas,
				dataType:'json',
				success: function(data) {
					closeAlert(load_alert);
					if(data.status) {
						window.parent.topMessagePrompt('success',data.msg);
						hideDialogModal();
						var t=JSON.parse(data.v);
						//改变文件夹is_have
						var r=JSON.parse(data.remark);
						if(r.is_add){
							$('.document-demo .img', parent.document).attr('data-id',t.id);
							$('.document-demo .delete', parent.document).attr('data-id',t.id);
							$('.document-demo .reset-name', parent.document).attr('data-id',t.id);
							$('.document-demo .title', parent.document).html(sub_str(t.name,6)).attr('title',t.name);
							$('.document-box', parent.document).append($('.document-demo', parent.document).html());
						}
						else{
							$('.document-box .reset-name', parent.document).each(function(){
								if($(this).data('id')==t.id){
									$(this).parents('.document-content').find('.title').html(sub_str(t.name,6)).attr('title',t.name);
								}
							});
						}
						$('.folder-content .img', parent.document).each(function(){
							if($(this).data('id')==$('#now-folder-id', parent.document).val()){
								$(this).attr('src',r.folder_img);
							}
						});
					} else {
						window.parent.topMessagePrompt('error',data.msg);
					}
				}
			});
		});
	}
	else{
		var load_alert=loadAlert('保存中...');
		datas.name='';
		$.ajax({
			url: "Admin/SupervisionOther/tableSave2",
			type: "post",
			data: datas,
			dataType:'json',
			success: function(data) {
				closeAlert(load_alert);
				if(data.status) {
					window.parent.topMessagePrompt('success',data.msg);
					hideDialogModal();
					var t=JSON.parse(data.v);
					//改变文件夹is_have
					var r=JSON.parse(data.remark);
					if(r.is_add){
						$('.document-demo .img', parent.document).attr('data-id',t.id);
						$('.document-demo .delete', parent.document).attr('data-id',t.id);
						$('.document-demo .reset-name', parent.document).attr('data-id',t.id);
						$('.document-demo .title', parent.document).html(sub_str(t.name,6)).attr('title',t.name);
						$('.document-box', parent.document).append($('.document-demo', parent.document).html());
					}
					else{
						$('.document-box .reset-name', parent.document).each(function(){
							if($(this).data('id')==t.id){
								$(this).parents('.document-content').find('.title').html(sub_str(t.name,6)).attr('title',t.name);
							}
						});
					}
					$('.folder-content .img', parent.document).each(function(){
						if($(this).data('id')==$('#now-folder-id', parent.document).val()){
							$(this).attr('src',r.folder_img);
						}
					});
				} else {
					window.parent.topMessagePrompt('error',data.msg);
				}
			}
		});
	}

	
	
}



function getWeek(date_str){
	if(date_str!=''){
		var arys1 = date_str.split('-');     //日期为输入日期，格式为 2013-3-10
		var ssdate = new Date(arys1[0], parseInt(arys1[1] - 1), arys1[2]);
		var week1=String(ssdate.getDay()).replace("0","日").replace("1","一").replace("2","二").replace("3","三").replace("4","四").replace("5","五").replace("6","六")//就是你要的星期几
		var week="星期"+week1; //就是你要的星期几
		return week;
	}
	else{
		return '';
	}
}